﻿@model OrganizationEditModel
@{
    ViewData["Title"] = "Organization: " + Model.Organization.Name;
}

@section Scripts {
    <script>
        (() => {
            document.getElementById('enterprise-trial').addEventListener('click', () => {
                if (document.getElementById('@(nameof(Model.PlanType))').value !==
                    '@((byte)Bit.Core.Enums.PlanType.Free)') {
                    alert('Organization is not on a free plan.');
                    return;
                }

                // Plan
                document.getElementById('@(nameof(Model.PlanType))').value =
                    '@((byte)Bit.Core.Enums.PlanType.EnterpriseAnnually)';
                document.getElementById('@(nameof(Model.Plan))').value = 'Enterprise (Trial)';
                document.getElementById('@(nameof(Model.Seats))').value = '10';
                document.getElementById('@(nameof(Model.MaxCollections))').value = '';
                document.getElementById('@(nameof(Model.MaxStorageGb))').value = '1';
                // Features
                document.getElementById('@(nameof(Model.UseGroups))').checked = true;
                document.getElementById('@(nameof(Model.UseDirectory))').checked = true;
                document.getElementById('@(nameof(Model.UseEvents))').checked = true;
                document.getElementById('@(nameof(Model.UsersGetPremium))').checked = true;
                document.getElementById('@(nameof(Model.UseTotp))').checked = true;
                document.getElementById('@(nameof(Model.Use2fa))').checked = true;
                document.getElementById('@(nameof(Model.UseApi))').checked = true;
                document.getElementById('@(nameof(Model.SelfHost))').checked = true;
                // Licensing
                document.getElementById('@(nameof(Model.LicenseKey))').value = '@Model.RandomLicenseKey';
                document.getElementById('@(nameof(Model.ExpirationDate))').value = '@Model.FourteenDayExpirationDate';
            });

            document.getElementById('@(nameof(Model.PlanType))').addEventListener('change', () => {
                const selectEl = document.getElementById('@(nameof(Model.PlanType))');
                const selectText = selectEl.options[selectEl.selectedIndex].text;
                document.getElementById('@(nameof(Model.Plan))').value = selectText;
            });

            document.getElementById('gateway-customer-link').addEventListener('click', () => {
                const gateway = document.getElementById('@(nameof(Model.Gateway))');
                const customerId = document.getElementById('@(nameof(Model.GatewayCustomerId))');
                if (!gateway || gateway.value === '' || !customerId || customerId.value === '') {
                    return;
                }

                if (gateway.value === '@((byte)Bit.Core.Enums.GatewayType.Stripe)') {
                    window.open('https://dashboard.stripe.com/customers/' + customerId.value, '_blank');
                } else if (gateway.value === '@((byte)Bit.Core.Enums.GatewayType.Braintree)') {
                    window.open('https://www.braintreegateway.com/merchants/@(Model.BraintreeMerchantId)/'
                        + customerId.value, '_blank');
                }
            });

            document.getElementById('gateway-subscription-link').addEventListener('click', () => {
                const gateway = document.getElementById('@(nameof(Model.Gateway))');
                const subId = document.getElementById('@(nameof(Model.GatewaySubscriptionId))');
                if (!gateway || gateway.value === '' || !subId || subId.value === '') {
                    return;
                }

                if (gateway.value === '@((byte)Bit.Core.Enums.GatewayType.Stripe)') {
                    window.open('https://dashboard.stripe.com/subscriptions/' + subId.value, '_blank');
                } else if (gateway.value === '@((byte)Bit.Core.Enums.GatewayType.Braintree)') {
                    window.open('https://www.braintreegateway.com/merchants/@(Model.BraintreeMerchantId)/' +
                        'subscriptions/' + subId.value, '_blank');
                }
            });
        })();
    </script>
}

<h1>Organization <small>@Model.Organization.Name</small></h1>

<h2>Organization Information</h2>
@await Html.PartialAsync("_ViewInformation", Model)
<h2>Billing Information</h2>
@await Html.PartialAsync("_BillingInformation",
    new BillingInformationModel { BillingInfo = Model.BillingInfo, OrganizationId = Model.Organization.Id })
<form method="post" id="edit-form">
    <h2>General</h2>
    <div class="row">
        <div class="col-sm">
            <div class="form-group">
                <label asp-for="Name"></label>
                <input type="text" class="form-control" asp-for="Name" required>
            </div>
        </div>
    </div>
    <div class="form-check mb-3">
        <input type="checkbox" class="form-check-input" asp-for="Enabled">
        <label class="form-check-label" asp-for="Enabled"></label>
    </div>
    <h2>Business Information</h2>
    <div class="row">
        <div class="col-sm">
            <div class="form-group">
                <label asp-for="BusinessName"></label>
                <input type="text" class="form-control" asp-for="BusinessName">
            </div>
        </div>
    </div>
    <h2>Plan</h2>
    <div class="row">
        <div class="col-sm">
            <div class="form-group">
                <label asp-for="PlanType"></label>
                <select class="form-control" asp-for="PlanType"
                        asp-items="Html.GetEnumSelectList<Bit.Core.Enums.PlanType>()"></select>
            </div>
        </div>
        <div class="col-sm">
            <div class="form-group">
                <label asp-for="Plan"></label>
                <input type="text" class="form-control" asp-for="Plan" required>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm">
            <div class="form-group">
                <label asp-for="Seats"></label>
                <input type="number" class="form-control" asp-for="Seats" min="1">
            </div>
        </div>
        <div class="col-sm">
            <div class="form-group">
                <label asp-for="MaxCollections"></label>
                <input type="number" class="form-control" asp-for="MaxCollections" min="1">
            </div>
        </div>
        <div class="col-sm">
            <div class="form-group">
                <label asp-for="MaxStorageGb"></label>
                <input type="number" class="form-control" asp-for="MaxStorageGb" min="1">
            </div>
        </div>
    </div>
    <h2>Features</h2>
    <div class="form-check">
        <input type="checkbox" class="form-check-input" asp-for="UseTotp">
        <label class="form-check-label" asp-for="UseTotp"></label>
    </div>
    <div class="form-check mb-2">
        <input type="checkbox" class="form-check-input" asp-for="SelfHost">
        <label class="form-check-label" asp-for="SelfHost"></label>
    </div>
    <div class="form-check">
        <input type="checkbox" class="form-check-input" asp-for="Use2fa">
        <label class="form-check-label" asp-for="Use2fa"></label>
    </div>
    <div class="form-check">
        <input type="checkbox" class="form-check-input" asp-for="UseApi">
        <label class="form-check-label" asp-for="UseApi"></label>
    </div>
    <div class="form-check">
        <input type="checkbox" class="form-check-input" asp-for="UseGroups">
        <label class="form-check-label" asp-for="UseGroups"></label>
    </div>
    <div class="form-check">
        <input type="checkbox" class="form-check-input" asp-for="UseDirectory">
        <label class="form-check-label" asp-for="UseDirectory"></label>
    </div>
    <div class="form-check">
        <input type="checkbox" class="form-check-input" asp-for="UseEvents">
        <label class="form-check-label" asp-for="UseEvents"></label>
    </div>
    <div class="form-check mb-3">
        <input type="checkbox" class="form-check-input" asp-for="UsersGetPremium">
        <label class="form-check-label" asp-for="UsersGetPremium"></label>
    </div>
    <h2>Licensing</h2>
    <div class="row">
        <div class="col-sm">
            <div class="form-group">
                <label asp-for="LicenseKey"></label>
                <input type="text" class="form-control" asp-for="LicenseKey">
            </div>
        </div>
        <div class="col-sm">
            <div class="form-group">
                <label asp-for="ExpirationDate"></label>
                <input type="datetime-local" class="form-control" asp-for="ExpirationDate">
            </div>
        </div>
    </div>
    <h2>Billing</h2>
    <div class="row">
        <div class="col-sm">
            <div class="form-group">
                <label asp-for="BillingEmail"></label>
                <input type="email" class="form-control" asp-for="BillingEmail">
            </div>
        </div>
        <div class="col-sm">
            <div class="form-group">
                <div class="form-group">
                    <label asp-for="Gateway"></label>
                    <select class="form-control" asp-for="Gateway"
                            asp-items="Html.GetEnumSelectList<Bit.Core.Enums.GatewayType>()">
                        <option value="">--</option>
                    </select>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm">
            <div class="form-group">
                <label asp-for="GatewayCustomerId"></label>
                <div class="input-group">
                    <input type="text" class="form-control" asp-for="GatewayCustomerId">
                    <div class="input-group-append">
                        <button class="btn btn-secondary" type="button" id="gateway-customer-link">
                            <i class="fa fa-external-link"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-sm">
            <div class="form-group">
                <label asp-for="GatewaySubscriptionId"></label>
                <div class="input-group">
                    <input type="text" class="form-control" asp-for="GatewaySubscriptionId">
                    <div class="input-group-append">
                        <button class="btn btn-secondary" type="button" id="gateway-subscription-link">
                            <i class="fa fa-external-link"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
<div class="d-flex mt-4">
    <button type="submit" class="btn btn-primary" form="edit-form">Save</button>
    <div class="ml-auto d-flex">
        <button class="btn btn-secondary mr-2" type="button" id="enterprise-trial">
            Enterprise Trial
        </button>
        <form asp-action="Delete" asp-route-id="@Model.Organization.Id"
              onsubmit="return confirm('Are you sure you want to delete this organization (@Model.Organization.Name)?')">
            <button class="btn btn-danger" type="submit">Delete</button>
        </form>
    </div>
</div>
